package com.diaohw.platform.framework.db.generator;

import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * 代码生成器支持自定义[DTO\VO等]模版
 */
public class CustomerFileEngine {
    static final Function<TableInfo, String> function = tableInfo -> tableInfo.getEntityName().replace("DO", "");

    public static List<CustomFile> load() {
        String[][] customerFiles = new String[][]{
                {"obj.dto", "DTO.java", "/template/entityDTO.java.ftl"},
                {"obj.vo", "VO.java", "/template/entityVO.java.ftl"},
                {"obj.convert", "Convert.java", "/template/convert.java.ftl"},
                {"obj.query", "Query.java", "/template/entityQuery.java.ftl"}
        };
        return Arrays.stream(customerFiles).map(str -> createCustomFile(str[0], str[1], str[2])).toList();
    }

    /**
     * 模板生成前处理
     */
    public static void beforeCustomerProcess(TableInfo tableInfo, Map<String, Object> stringObjectMap) {
        Map<String, String> packageMap = new HashMap<>((Map<String, String>) stringObjectMap.get("package"));
        // 设置包名
        String entityPkg = packageMap.get("Entity");
        packageMap.put("Dto", entityPkg.replace("entity", "dto"));
        packageMap.put("Vo", entityPkg.replace("entity", "vo"));
        packageMap.put("queryPkg", entityPkg.replace("entity", "query"));
        packageMap.put("Convert", entityPkg.replace("entity", "convert"));

        stringObjectMap.put("package", packageMap);
        // 实体类后缀
        String entityName = function.apply(tableInfo);
        String convertName = entityName + "Convert";
        String dtoName = entityName + "DTO";
        String queryName = entityName + "Query";
        String voName = entityName + "VO";

        stringObjectMap.put("convert", convertName);
        stringObjectMap.put("dto", dtoName);
        stringObjectMap.put("query", queryName);
        stringObjectMap.put("vo", voName);

        for (TableField field : tableInfo.getFields()) {
            if (field.getColumnType() == DbColumnType.BIG_DECIMAL){
                String name = field.getPropertyName();
                field.setPropertyName(name,DbColumnType.DOUBLE);
            }
        }


    }

    /**
     * 构建自定义文件
     *
     * @param packageName  包名
     * @param fileName     文件名称
     * @param templatePath 模版路径
     */
    private static CustomFile createCustomFile(String packageName, String fileName, String templatePath) {
        return new CustomFile.Builder()
                .packageName(packageName)
                .fileName(fileName)
                .templatePath(templatePath)
                .enableFileOverride()
                .formatNameFunction(function)
                .build();
    }

}